﻿// -----------------------------------------------------------------------
// <copyright file="AdminElementCollection.cs" company="EPAM Systems">
////Copyright (c) EPAM Systems, 2012. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

namespace MBOP.Configuration.ConfigElements
{
	#region Usings

	using System;
	using System.Collections.Generic;
	using System.Configuration;
	using System.Linq;
	using System.Text;

	#endregion

	/// <summary>
	/// Provides the collection of the admins items.
	/// </summary>
	[ConfigurationCollection(typeof(AdminElement))]
	public class AdminElementCollection : ConfigurationElementCollection
	{
		#region Indexers

		/// <summary>
		/// Gets configuration element by specified index.
		/// </summary>
		/// <param name="index">The specified index.</param>
		/// <returns>The admin element</returns>
		public AdminElement this[int index]
		{
			get
			{
				return (AdminElement)BaseGet(index);
			}
		}

		#endregion

		#region Public Methods

		/// <summary>
		/// Returns a string that represents the current object.
		/// </summary>
		/// <returns>A string that represents the current object.</returns>
		public override string ToString()
		{
			StringBuilder admins = new StringBuilder();

			foreach (var adminKey in this.BaseGetAllKeys())
			{
				AdminElement element = BaseGet(adminKey) as AdminElement;

				admins.Append(element.Token);
				admins.AppendLine(";");
			}

			return admins.ToString();
		}

		#endregion

		#region Protected Methods

		/// <summary>
		/// Creates new configuration element.
		/// </summary>
		/// <returns>Configuration element.</returns>
		protected override ConfigurationElement CreateNewElement()
		{
			return new AdminElement();
		}

		/// <summary>
		/// Gets the configuration element key.
		/// </summary>
		/// <param name="element">The configuration element.</param>
		/// <returns>The key.</returns>
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((AdminElement)element).Token;
		}

		#endregion
	}
}
